package com.jelly.web.modules.basics.bulletin.service.impl;

import com.jelly.common.util.StringUtils;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.config.webSocket.WebSocket;
import com.jelly.web.modules.basics.bulletin.dao.BulletinMapper;
import com.jelly.web.modules.basics.bulletin.domain.Bulletin;
import com.jelly.web.modules.basics.bulletin.service.IBulletinService;
import com.jelly.web.modules.message.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * @创建人 ql
 * @创建时间 2018-09-29 14:16
 * @项目名称 jelly
 * @功能描述: 公告接口实现类
 **/
@Service
public class BulletinServiceImpl implements IBulletinService {

    @Autowired
    private BulletinMapper bulletinMapper;
    /**
     * 新增公告
     * @param bulletin
     * @return
     */
    @Override
    public boolean addBulletin(Bulletin bulletin) {

        bulletin.setBulletinId(UUID.randomUUID().toString().replaceAll("-", ""));

        bulletin.setAddDate(new Date());

        bulletin.setAddId(TokenUtil.getUserId());
        boolean bol = bulletinMapper.insertSelective(bulletin)>0;
        //公告、通知创建成功后群发消息
        if(bol){
            WebSocket.sendMessage(bulletin);
        }
        return bol;

    }
    /**
     * 修改公告
     * @param bulletin
     * @return
     */
    @Override
    public boolean updatBulletin(Bulletin bulletin) {

        if(StringUtils.isBlank(bulletin.getBulletinId())){
            bulletin.setBulletinId(bulletin.getBaseId());
        }

        bulletin.setUpdateDate(new Date());

        bulletin.setUpdateId(TokenUtil.getUserId());

        return bulletinMapper.updateByPrimaryKeySelective(bulletin) > 0;

    }
    /**
     * 删除公告
     * @param bulletinId
     * @return
     */
    @Override
    public boolean delectBulletin(String bulletinId) {

        return bulletinMapper.deleteByPrimaryKey(bulletinId) > 0;

    }

}
